package com.example.safelib

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.baselib.activity.BaseViewModelActivity
import com.example.baselib.constants.ARConstants
import com.example.safelib.adapter.SafeAllNickAdapter
import com.example.safelib.bean.SafeNickBean
import kotlinx.android.synthetic.main.activity_all_safe.*
import kotlinx.android.synthetic.main.fragment_safe_alarm.*
import java.lang.Exception

@Route(path = ARConstants.APP_SAFE_NICK)
class SafeAllNickActivity : BaseViewModelActivity<SafeAllViewModel>() {
    private lateinit var safeAllNickAdapter: SafeAllNickAdapter
    override fun providerVMClass(): Class<SafeAllViewModel>? {
        return SafeAllViewModel::class.java
    }

    override fun layoutResId(): Int =R.layout.activity_all_safe

    override fun initView() {
        setToolbarMsg("安全监控")
        setToolbarColor(R.color.colorDBlue, R.color.colorWhite)
        setToolbarMenu(true, R.id.menu_safe)
        safeAllNickAdapter =SafeAllNickAdapter(R.layout.safe_all_nick_item)
        val empty = LayoutInflater.from(this).inflate(R.layout.empty_item, null, false)
        safeAllNickAdapter.emptyView=empty
        safe_all_nick_rcv.adapter=safeAllNickAdapter
        safeAllNickAdapter.bindToRecyclerView(safe_all_nick_rcv)
        safe_all_nick_rcv.layoutManager =  LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false)
        safeAllNickAdapter.setOnItemClickListener { adapter, view, position ->
            val safeNickBean = adapter.getItem(position) as SafeNickBean
            val intent= Intent(this,SafeMainActivity::class.java)
            intent.putExtra("unitname",safeNickBean.minename)
            intent.putExtra("unitcode",safeNickBean.minecode)
            startActivity(intent)
        }
        safe_all_nick_srl.setColorSchemeColors(resources.getColor(R.color.colorDBlue))
        safe_all_nick_srl.setOnRefreshListener {
            viewModel.isLoad=false
            viewModel.loadSafeNickData() }

    }

    override fun requestFinally(it: Int?) {
        super.requestFinally(it)
        safe_all_nick_srl.isRefreshing=false
    }

    override fun requestError(it: Exception?) {
        safe_all_nick_empty.visibility=View.VISIBLE
        if (it?.message=="暂无数据"){
            safe_all_nick_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            safe_all_nick_empty.findViewById<TextView>(R.id.err_msg).visibility= View.INVISIBLE
//            safe_alarm_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            safe_all_nick_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        }
        safe_all_nick_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadSafeNickData() }


    }
    override fun initData() {
        viewModel.getSafeNickData().observe(this, Observer {
            safeAllNickAdapter.setNewData(it)
            safe_all_nick_empty.visibility=View.INVISIBLE
        })
    }

}
